package com.hnu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hnu.common.Constants;
import com.hnu.dto.Result;
import com.hnu.entity.Admin;
import com.hnu.entity.Category;
import com.hnu.service.AdminService;
import com.hnu.service.CategoryService;
import com.hnu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author hj
 * @Date 2023/12/20 8:49
 * @PackageName:com.hnu.controller
 * @ClassName: AdminController
 * @Description: create a class
 * @Version 1.0
 */
@CrossOrigin //服务器端解决跨域
@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加新的分类
     * @param category
     * @return
     */
    @PostMapping("/save")
    public Result add(@RequestBody Category category) {
       if(category.getId() == null){
           category.setGrade(1);
           category.setIsLeaf((byte) 0);
           category.setCategoryParentId(0L);
           category.setId(null);
       }else {
           category.setGrade(category.getGrade() + 1);
           category.setIsLeaf((byte) 1);
           category.setCategoryParentId(category.getId());
       }
        category.setCategoryCreateTime(new Date());
        category.setCategoryUpdateTime(new Date());
        categoryService.save(category);
        return Result.ok(Constants.ADD_SUCCESS);
    }

    /**
     * 查询所有分类
     * @return
     */
    @PostMapping("/findAll")
    public Result findAll() {
        List<Category> list = categoryService.list();
       /* List<Category> collectRoot = Optional.ofNullable(list).orElse(new ArrayList<>()).stream()
                .filter(category -> category.getCategoryParentId() == 0).collect(Collectors.toList());
        for (Category category : collectRoot) {
            category.setCategoryChild(getMainChildern(category.getId(),list));
        }*/
        return Result.ok(Constants.OK).data(list);
    }


    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable long id){
        int flag = categoryService.delete(id);
        if (flag == 0){
            return Result.error(Constants.DELETE_ERROR);
        }
        return Result.ok(Constants.DELETE_OK).data(flag);
    }


    /**
     * 递归获取孩子
     * @param id
     * @param list
     * @return
     */
    private List<Category> getMainChildern(Long id, List<Category> list) {
        return Optional.ofNullable(list).orElse(new ArrayList<>()).stream()
                .filter(category -> (category.getCategoryParentId()== id ))
                .map(category -> {
                    category.setCategoryChild(getMainChildern(category.getId(),list));
                    if (category.getCategoryChild() != null && category.getCategoryChild().size() > 0){
                        category.getCategoryChild().sort(Comparator.comparing(obj->obj.getId()));
                    }
                    return category;
                })
                .collect(Collectors.toList());
    }


}
